// Shopper use-a Cart

// Shopper has-a Cart
#include <iostream>
#include <string>
using namespace std;

// 物品
struct Item {
    string name;
    float price;
};

// 购物车
class Cart {
private:
    Item stuff[50];
    int howMany;
    int whichItem;

public:
    Cart() {
        howMany = 0;
        whichItem = 0;
    }

    void AddItemToCart(Item& newthing);
    Item RetrieveNextItem();
    int HowManyInCart() { return howMany; }
};

// 顾客
class Shopper {
private:
    float wallet;

public:
    Shopper() { wallet = 100.0f; }

    float GetMoneyFromWallets(float cost);

    void PutItemInCard(Item& newthing, Cart* cart);
};

// 收银员
class Clerk {
private:
    float total;

public:
    Clerk() { total = 0.0f; }

    float FigureTotal(Cart* cart);
};

int main() {
    Shopper ed;
    Clerk gladys;
    Cart squeakyWheels;

    Item item;

    item.name = "potato chips"; // 炸薯片
    item.price = 2.99f;
    ed.PutItemInCard(item, &squeakyWheels);

    item.name = "coffee";
    item.price = 8.99f;
    ed.PutItemInCard(item, &squeakyWheels);

    float total = gladys.FigureTotal(&squeakyWheels);
    cout << "ed 应付给 gladys " << total << "\n";

    float cost = ed.GetMoneyFromWallets(total);
    cout << "ed 可以付给 gladys " << cost << "\n";
    cout << "购物真辛苦\n";
}

void Cart::AddItemToCart(Item& newthing) {
    stuff[howMany++] = newthing;
}

Item Cart::RetrieveNextItem() {
    return stuff[whichItem++];
}

float Shopper::GetMoneyFromWallets(float cost) {
    if (cost > wallet) {
        cout << "钱不够用! 退回一些物品!\n";
        return 0;
    }

    wallet -= cost;
    return cost;
}

void Shopper::PutItemInCard(Item& newthing, Cart* cart) {
    cart->AddItemToCart(newthing);
}

float Clerk::FigureTotal(Cart* cart) {
    float total = 0.0f;

    int n = cart->HowManyInCart();
    for (int i = 0; i < n; i++) {
        total += cart->RetrieveNextItem().price;
    }

    return total;
}